package com.yunpeng.aidevice.task;

import com.yunpeng.aidevice.common.dto.cache.UpdateRelationDTO;
import com.yunpeng.aidevice.common.enums.RelationValueEnum;
import com.yunpeng.common.dto.Result;
import com.yunpeng.task.BaseTask;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * RelationValueUpdateTask 管理表更新任务
 *
 * @author : Moore(hemw)
 * @date : 2020-04-25
 */
public class RelationValueUpdateTask extends BaseTask {

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(RelationValueUpdateTask.class);

    public RelationValueUpdateTask() {
    }

    public RelationValueUpdateTask(Long currentTimes) {
        super(currentTimes);
    }

    @Override
    public void run() {
        Result<UpdateRelationDTO> updateRelationDtoResult = InternalServiceManager.INSTANCE.getRelationValueUpdateQueueService().pop();
        if (updateRelationDtoResult == null || !updateRelationDtoResult.isSuccess()) {
            LOGGER.info("get update msg is empty or error,{}", updateRelationDtoResult);
            return;
        }
        UpdateRelationDTO updateRelationDTO = updateRelationDtoResult.getData();
        if (updateRelationDTO == null) {
            return;
        }
        if (StringUtils.isBlank(updateRelationDTO.getUpdateTable())
                || StringUtils.isBlank(updateRelationDTO.getUpdateTableCodeValue())
                || updateRelationDTO.getUpdateKeyValue() == null
                || updateRelationDTO.getUpdateKeyValue().isEmpty()
        ) {
            return;
        }
        try {
            //更新用户关联表
            if (updateRelationDTO.getUpdateTable().equalsIgnoreCase(RelationValueEnum.user.getName())) {
                String userName = (String) updateRelationDTO.getUpdateKeyValue().get("name");
                String phone = (String) updateRelationDTO.getUpdateKeyValue().get("phone");
                String email = (String) updateRelationDTO.getUpdateKeyValue().get("email");
                InternalServiceManager.INSTANCE.getCommonMapper().updateUserNameRelation(updateRelationDTO.getUpdateTableCodeValue(), userName, phone, email);
                return;
            }
            //更新组织关联表
            if (updateRelationDTO.getUpdateTable().equalsIgnoreCase(RelationValueEnum.org.getName())) {
                String orgName = (String) updateRelationDTO.getUpdateKeyValue().get("name");
                String path = (String) updateRelationDTO.getUpdateKeyValue().get("path");
                InternalServiceManager.INSTANCE.getCommonMapper().updateOrgNameRelation(updateRelationDTO.getUpdateTableCodeValue(), orgName, path);
                return;
            }
            //更新商户默认用户
            if (updateRelationDTO.getUpdateTable().equalsIgnoreCase(RelationValueEnum.merchantDefaultUser.getName())) {
                String userName = (String) updateRelationDTO.getUpdateKeyValue().get("name");
                String phone = (String) updateRelationDTO.getUpdateKeyValue().get("phone");
                String email = (String) updateRelationDTO.getUpdateKeyValue().get("email");
                String merchantCode = (String) updateRelationDTO.getUpdateKeyValue().get("merchantCode");
                String tenantCode = (String) updateRelationDTO.getUpdateKeyValue().get("tenantCode");
                Boolean updateMerchant = (Boolean) updateRelationDTO.getUpdateKeyValue().get("updateMerchant");
                if (updateMerchant) {
                    InternalServiceManager.INSTANCE.getCommonMapper().updateMerchantUser(userName, phone, email, tenantCode, merchantCode);
                } else {
                    InternalServiceManager.INSTANCE.getCommonMapper().updateMerchantDefaultUser(userName, phone, email, tenantCode, merchantCode);
                }
                return;
            }
            if (updateRelationDTO.getUpdateTable().equalsIgnoreCase(RelationValueEnum.userOrg.getName())) {
                String orgName = (String) updateRelationDTO.getUpdateKeyValue().get("orgName");
                String orgCode = (String) updateRelationDTO.getUpdateKeyValue().get("orgCode");
                InternalServiceManager.INSTANCE.getCommonMapper().updateUserOrgRelation(orgCode, orgName, updateRelationDTO.getUpdateTableCodeValue());
                return;
            }
        } catch (Exception ex) {
            LOGGER.error("execute relation update error,", ex);
        }
    }
}
